package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.Configuracion;

public class ConfiguracionDAOImpl extends JdbcDaoSupport implements IConfiguracionDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByGrupoQuery findByGrupoQuery;
    
    private FindByGrupoAndValorQuery findByGrupoAndValorQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM ADM_CONFIGURACION_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(Configuracion configuracion) {
		deleteOperation.execute( configuracion  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public Configuracion findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(Configuracion configuracion) {
		insertOperation.execute( configuracion );
	}

	public void update(Configuracion configuracion) {
		updateOperation.execute( configuracion );
	}

	public Configuracion findByCriteria(Configuracion  configuracion) {
		return findByCriteriaQuery.runQuery(configuracion);
	}

	public List load(Configuracion configuracionSearch) {
		return findAllQuery.runQuery(configuracionSearch);
	}
	
	public List loadByGrupo(Configuracion configuracionSearch) {
		return findByGrupoQuery.runQuery(configuracionSearch);
	}
	
	public List loadByGrupoAndValor(Configuracion configuracionSearch) {
		return findByGrupoAndValorQuery.runQuery(configuracionSearch);
	}

	public Configuracion findByActiveState(Configuracion  configuracion) {
		return findByActiveStateQuery.runQuery(configuracion);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByGrupoAndValorQuery = new FindByGrupoAndValorQuery( getDataSource() );
    	//Disabled
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO ADM_CONFIGURACION_T ("
				  + " ID, "
				  + " GRUPO, "
				  + " PARAMETRO, "
				  + " VALOR, "
				  + " DESCRIPCION, "
				  + " MODIFICABLE, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " ADM_CONFIGURACION_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(Configuracion configuracion) {
			InsertOperation.this.update( new Object[]{
				configuracion.getGrupo(),
				configuracion.getParametro(),
				configuracion.getValor(),
				configuracion.getDescripcion(),
				configuracion.getModificable(),
				configuracion.getEstado(),
				configuracion.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE ADM_CONFIGURACION_T SET "
					  + " GRUPO = ? ,"
					  + " PARAMETRO = ? ,"
					  + " VALOR = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " MODIFICABLE = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(Configuracion configuracion) {
			UpdateOperation.this.update( new Object[]{
				configuracion.getGrupo(),
				configuracion.getParametro(),
				configuracion.getValor(),
				configuracion.getDescripcion(),
				configuracion.getModificable(),
				configuracion.getEstado(),
				configuracion.getUsuarioModifica(),
				configuracion.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_CONFIGURACION_T "
										+ WHERE 
										+ " 1=1 ";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(Configuracion configuracion) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM ADM_CONFIGURACION_T "
										+ WHERE 
										+ " 1=1 ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			Configuracion configuracion = new Configuracion();
			configuracion.setId(new Long(rs.getLong("ID")));
			configuracion.setGrupo(rs.getString("GRUPO"));
			configuracion.setParametro(rs.getString("PARAMETRO"));
			configuracion.setValor(rs.getString("VALOR"));
			configuracion.setDescripcion(rs.getString("DESCRIPCION"));
			configuracion.setModificable(rs.getString("MODIFICABLE"));
			configuracion.setEstado(rs.getString("ESTADO"));
			configuracion.setUsuarioCrea(rs.getString("USUARIO_CREA"));
			configuracion.setFechaCrea(rs.getDate("FECHA_CREA"));
			configuracion.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
			configuracion.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
			return configuracion;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 
										+ " 1=1 ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Configuracion runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (Configuracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND GRUPO = ? "
			  	  	  					//+ " AND PARAMETRO = ? "
			  	  	  					//+ " AND VALOR = ? "
			  	  	  					//+ " AND DESCRIPCION = ? "
			  	  	  					//+ " AND MODIFICABLE = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public Configuracion runQuery(Configuracion configuracion) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//configuracion.getId(),
				//configuracion.getGrupo(),
				//configuracion.getParametro(),
				//configuracion.getValor(),
				//configuracion.getDescripcion(),
				//configuracion.getModificable(),
				//configuracion.getEstado(),
				//configuracion.getUsuarioCrea(),
				//configuracion.getFechaCrea(),
				//configuracion.getUsuarioModifica(),
				//configuracion.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (Configuracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 
										+ " 1=1 "
										+ " AND UPPER(GRUPO) LIKE ? "
										+ " AND UPPER(PARAMETRO) LIKE ? "
										+ " AND UPPER(DESCRIPCION) LIKE ? "
										+ " AND ESTADO LIKE ? "
										+ " ORDER BY ID ASC";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
			declareParameter( new SqlParameter(Types.CHAR) );
    		compile();
    	}
    	public List runQuery(Configuracion configuracionSearch) {
			List result = execute(new Object[] {
					new String("%"
							+ (configuracionSearch.getGrupo() != null
									&& !configuracionSearch.getGrupo().equals(
											"") ? configuracionSearch
									.getGrupo().toUpperCase() : "%") + "%"),
					new String("%"
							+ (configuracionSearch.getParametro() != null
									&& !configuracionSearch.getParametro()
											.equals("") ? configuracionSearch
									.getParametro().toUpperCase() : "%") + "%"),
					new String("%"
							+ (configuracionSearch.getDescripcion() != null
									&& !configuracionSearch.getDescripcion()
											.equals("") ? configuracionSearch
									.getDescripcion().toUpperCase() : "%")
							+ "%"), new String(configuracionSearch.getEstado()) 
					});
			if (result.size() > 0) {
				return result;
			} else {
				return null;
			}
    	}
    }

 // ------------- FindAllQuery ----------------------------------------    
    class FindByGrupoQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 										
										+ " UPPER(GRUPO) LIKE ? "
										+ " ORDER BY ID ASC";
    	public FindByGrupoQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );			
    		compile();
    	}
    	public List runQuery(Configuracion configuracionSearch) {
			List result = execute(new Object[] {
					new String("%" + (configuracionSearch.getGrupo() != null && !configuracionSearch.getGrupo().equals(
								"") ? configuracionSearch.getGrupo().toUpperCase() : "%") + "%")					
					});
			if (result.size() > 0) {
				return result;
			} else {
				return null;
			}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindByGrupoAndValorQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE 										
										+ " UPPER(GRUPO) LIKE ?  AND ? < VALOR"
										+ " ORDER BY ID ASC";
    	public FindByGrupoAndValorQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );	
			declareParameter( new SqlParameter(Types.INTEGER) );	
    		compile();
    	}
    	public List runQuery(Configuracion configuracionSearch) {
			List result = execute(new Object[] {
					new String("%" + (configuracionSearch.getGrupo() != null && !configuracionSearch.getGrupo().equals(
								"") ? configuracionSearch.getGrupo().toUpperCase() : "%") + "%"),
					configuracionSearch.getValor()
					});
			if (result.size() > 0) {
				return result;
			} else {
				return null;
			}
    	}
    }

    
    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ESTADO = ? ";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public Configuracion runQuery(Configuracion configuracion) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (Configuracion) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
